﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using boe2.irc;

namespace wpfirc
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		private IrcConnection connection;
        private const Boolean DebugEnabled = true;
		private const String SystemChannel = "system";
		private List<String> knownsystemops = new List<string>(new String[] { "global", "auth", "chanserv" });
		private Dictionary<IrcChannel, IrcChannelControl> channels;

		public MainWindow()
		{
			InitializeComponent();
			channels = new Dictionary<IrcChannel, IrcChannelControl>();
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			connection = new IrcConnection("irc.tweakers.net", 6667);
			connection.Connected += new EventHandler(connection_Connected);
			connection.CommandSent += new IrcConnection.CommandSentEventHandler(connection_CommandSent);
			connection.DataReceived += new IrcConnection.DataReceivedEventHandler(connection_DataReceived);
			connection.NickName = "BoeTest";
			connection.Start();
		}

		private IrcChannelControl SendToTab(IrcMessage message)
		{
			if (message.MessageType == MessageType.PING)
			{
				connection.SendCommand(new IrcCommand("PONG testclient"));
                SendDebugMessage("PONG testclient");
			}
			IrcChannelControl channelControl;
			String target = message.Target == null ? SystemChannel : message.Target;
			if (target == connection.NickName)
			{
				target = message.Prefix != null ? message.Prefix.Nick != null ? message.Prefix.Nick : SystemChannel : SystemChannel;
			}
			if (knownsystemops.Contains(target.ToLower()))
			{
				target = SystemChannel;
			}
			channelControl = channels.Where(ch => ch.Key.Name == target).Select(ch => ch.Value).FirstOrDefault();
			if (channelControl == null)
			{
				IrcChannel channel = new IrcChannel(target);
				TabItem tab = new TabItem();
				tab.Header = target;
				channelControl = new IrcChannelControl(connection, channel);
				tab.Content = channelControl;
				TabControl.Items.Add(tab);
				channels.Add(channel, channelControl);
			}
            channelControl.SendMessage(message);
            if (DebugEnabled)
            {
                SendDebugMessage(message.RawMessage);
            }
			return channelControl;
		}

        private void SendDebugMessage(String message)
        {
			String debugChannelName = "DEBUG";
            IrcChannelControl channelControl;
			channelControl = channels.Where(ch => ch.Key.Name == debugChannelName).Select(ch => ch.Value).FirstOrDefault();
			if (channelControl == null)
            {
				IrcChannel channel = new IrcChannel(debugChannelName);
                TabItem tab = new TabItem();
				tab.Header = channel;
				channelControl = new IrcChannelControl(connection, channel);
                tab.Content = channelControl;
                TabControl.Items.Add(tab);
				channels.Add(channel, channelControl);
            }
            channelControl.SendText(message);
        }

		void connection_DataReceived(DataReceivedEventArgs args)
		{
			foreach (IrcMessage message in args.Data)
			{
				Dispatcher.Invoke(new Action(() => SendToTab(message)));
			}
		}

		void connection_CommandSent(CommandSentEventArgs args)
		{
			// TODO: implement general logic
		}

		void connection_Connected(object sender, EventArgs e)
		{
			Dispatcher.Invoke(new Action(() =>
			{
				connection.SendCommand(new IrcCommand("NICK " + connection.NickName));
				connection.SendCommand(new IrcCommand("USER TestConsole home @ @"));
				connection.SendCommand(new IrcCommand("JOIN #test"));
			}));
		}
	}
}
